package com.wcgksoft.common.util.Datacollection;


import java.nio.charset.StandardCharsets;

public class ConvertHex {

	/**
	 * @description 将16进制转换为二进制
	 * 
	 * @param hexStr
	 * @return
	 */
	public static byte[] parseHexStr2Byte(String hexStr) {
	    if (hexStr.length() < 1)
	        return null;
	        byte[] result = new byte[hexStr.length() / 2];
	        for (int i = 0; i < hexStr.length() / 2; i++) {
	            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
	            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
	            result[i] = (byte) (high * 16 + low);
	        }
	        return result;
	    }

	/**
	 * 16进制转成10进制
	 * @param hex
	 * @return
	 */
	public static int hexToDecimal(String hex) {
		int decimalValue = 0;
		for (int i = 0; i < hex.length(); i++) {
			char hexChar = hex.charAt(i);
			decimalValue = decimalValue * 16 + hexCharToDecimal(hexChar);
		}
		return decimalValue;
	}

	public static int hexCharToDecimal(char hexChar) {
		if (hexChar >= 'A' && hexChar <= 'F')
			return 10 + hexChar - 'A';
		else
			return hexChar - '0';// 切记不能写成int类型的0，因为字符'0'转换为int时值为48
	}
	
	/**
	 * 16进制转成明文
	 * @param hex
	 * @return
	 */
	public static String  convertHexToString(String  hex){
		//hex=hex.replace(" ", "");
		StringBuilder sb = new StringBuilder();
	    StringBuilder sb2 = new StringBuilder();
	    for( int i=0; i<hex.length()-1; i+=3 ){
	        String s = hex.substring(i, (i + 2));           
	        int decimal = Integer.parseInt(s,16);          
	        sb.append((char)decimal);
	        sb2.append(decimal);
	    }
	 
	    return sb.toString();
	}
	
	 /**
	 * byte[]数组转换为16进制的字符串
	 * 
	 * @param bytes 要转换的字节数组
	 * @return 转换后的结果
	 */
	public static String bytesToHexString(byte[] bytes) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < bytes.length; i++) {
			String hex = Integer.toHexString(0xFF & bytes[i]);
			if (hex.length() == 1) {
				sb.append('0');
			}
			sb.append(hex+" ");
		}
		return sb.toString();
	}
	
	// 转化十六进制编码为字符串
    public static String toStringHex2(String s) {
    	s=s.replace(" ", "");
       byte[] baKeyword = new byte[s.length() / 2];
       for (int i = 0; i < baKeyword.length; i++) {
        try {
         baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(
           i * 2, i * 2 + 2), 16));
        } catch (Exception e) {
         e.printStackTrace();
        }
       }
       try {
        s = new String(baKeyword, StandardCharsets.UTF_8);// UTF-16le:Not
       } catch (Exception e1) {
        e1.printStackTrace();
       }
       return s;
    }
    

    /**
	 * 将十六进制的字符串转换成字节数组
	 *
	 * @param hexString
	 * @return
	 */
    public static byte[] hexStringToBytes(String hexString) {
		if (hexString == null || hexString.equals("")) {
			return null;
		}
		// toUpperCase将字符串中的所有字符转换为大写
		hexString = hexString.toUpperCase();
		int length = hexString.length() / 2;
		// toCharArray将此字符串转换为一个新的字符数组。
		char[] hexChars = hexString.toCharArray();
		byte[] d = new byte[length];
		for (int i = 0; i < length; i++) {
			int pos = i * 2;
			d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
		}
		return d;
	}
    //charToByte返回在指定字符的第一个发生的字符串中的索引，即返回匹配字符
	private static byte charToByte(char c) {
		return (byte) "0123456789ABCDEF".indexOf(c);
	}
}
